﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using Frogism.Core;
namespace Frogism.Domain
{
    public class BookStats
    {
        public BookStats(IEnumerable<IBook> books)
        {
            _books = books;
        }
        private IEnumerable<IBook> _books;
        public IEnumerable<IBook> Books
        {
            get
            {
                if (_books == null)
                {
                    _books = new Collection<IBook>();
                }
                return _books;
            }
        }
        public double Goal
        {
            get
            {
                return GoalRate * DateTime.Now.DayOfYear;
            }
        }
        public int YearRead
        {
            get
            {
                
                return Books.Sum(b => b.YearRead);
            }
        }
        public double YearDailyAverage {
          get {
            return Math.Round(((double)YearRead / DateTime.Now.DayOfYear), 2);
          }
        }
        public int ReadToday {
          get {
            ///*
            IEnumerable<IBook> newbooks = Books.Where(b => b.WasReadToday);
            if (newbooks.Count() > 0) {
              int tempRead = 0;
              foreach (IBook b2 in newbooks) {
                int PageAsOfYesterday = b2.ReadBy(DateTime.Now.AddDays(-1));
                tempRead += b2.TotalRead - PageAsOfYesterday;
              }
              return tempRead;
            } else {
              return 0;
            }
            // */
          //  return Books.Where(b => b.WasReadToday).Sum(b => b.TotalRead - b.ReadBy(DateTime.Now.AddDays(-1).CleanDate()) );
          }
        }
        public double GoalRate {
          get{
            if (DateTime.Now.Year <= 2008)
              return 16.4;
            else
              return 19.2;
          }
        }
        public double DaysAhead {
          get {
            return Math.Round(Diff / GoalRate, 2);
          }
        }
        public double Diff {
          get {
            return YearRead - Goal;
          }
        }
        public Collection<StatItem> GetWeekDetails() {
          Collection<StatItem> items = new Collection<StatItem>();

          for (int i = 0; i < 7; i++) {
            int tempRead = 0;
            DateTime StatDate = DateTime.Now.AddDays(-i);
            foreach (IBook b2 in Books) {
              int PageAsOfDayBefore = b2.ReadBy(StatDate.AddDays(-1));
              int PageAsOfDay = b2.ReadBy(StatDate);
              tempRead += PageAsOfDay - PageAsOfDayBefore;
            }
            items.Add(new StatItem(StatDate.ToShortDateString(), tempRead));

          }
          
        
            return items;
        

        }
        public class StatItem {
          public StatItem(string name, double value) {
            Name = name;
            Value = value;
          }
          public double Value { get; set; }
          public string Name { get; set; }
        }
    }
}
